<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class Type extends MY_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this ->load->helper('url');
		$this ->load->database();
	}
	//分类显示
	public function index($offset='')
	{
	    $this->load->model('Type_model');

		//每页条数
		$page_size = 3;

		$this->load->library('pagination');
		$config['base_url'] = site_url('admin/Type/index');
		//$config['total_rows'] = $this->Type_model->countRows();
		$config['total_rows'] =$this->db->count_all_results('type');
		$config['per_page'] = $page_size;

		//自定义分页链接
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';

		//初始化分页类
		$this->pagination->initialize($config);
		//生成分页信息
		$arr['link'] = $this->pagination->create_links();
		$limit = $config['per_page'];

		$arr['type'] = $this->Type_model->chak($limit,$offset);
/*		$this->load->view('admin/commont/head');
		$this->load->view('admin/commont/left');*/
		$this->load->view('admin/type/index',$arr);
	}
	/**
	 * 分类添加
	 */
	public function add()
	{
		$method = !empty($_GET) ? 'GET' : 'POST';
		//上面只是用来判断用什么方式提交的，顺便还可以映射一下,使用的话就更方便了
		if(!empty($_POST)){
			$data = array(
				'type_name'=>$this->input->post('type_name')
				);
			 $this->load->model('Type_model');
			$arr = $this->Type_model->add($data);
			if(!$arr)
			{
				echo "<script>alert('添加失败')</script>";
				//redirect('admin/type/index');
			}
			redirect('admin/type/index');

		}
/*		$this->load->view('admin/commont/head');
		$this->load->view('admin/commont/left');*/
		$this->load->view('admin/type/add');
	}
	/**
	 * 分类修改
	 */
	public function update()
	{
		$id=$this->input->get('id');
		$this->load->model('Type_model');
		$arr = $this->Type_model->updas($id);
		//接受修改传过来的值
		$method = !empty($_GET) ? 'GET' : 'POST';
		//上面只是用来判断用什么方式提交的，顺便还可以映射一下,使用的话就更方便了
		if(!empty($_POST)){
				$id = $this->input->post('type_id');
				$name = $this->input->post('type_name');
				$data = array(
						'type_name'=>$name
					);
			$this->load->model('Type_model');
			$arr = $this->Type_model->update($id,$data);
			if(!$arr)
			{
				echo "修改失败";die();
			}
			redirect('admin/type/index');

		}
/*		$this->load->view('admin/commont/head');
		$this->load->view('admin/commont/left');*/
		$this->load->view('admin/type/update',$arr);
	}
	/**
	 * 删除
	 */
	public function del()
	{
		$id=$this->input->get('id');
		$this->load->model('Type_model');
		$arr = $this->Type_model->del($id);
		if(!$arr)
		{
			echo "删除失败";die();
		}
		redirect('admin/type/index');
	}

}





?>